package com.cgc.venus.server.log.service;

import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.utils.DateUtil;
import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.log.inter.ExLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 业哥 on 2017/11/20.
 */
@Service
public class ExLogServiceImpl extends AbstractBaseService implements ExLogService{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private final int PAGE_SIZE=10;


    @Override
    public List queryExModule() throws VenusException {
        String sql="select ex.module exmodule,group_concat(CONCAT(ex.`code`,':',ex.`comment`)) excode from t_ex_log_par ex group by module";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        List l1=new ArrayList();
        Map<String, Object> mTmp0=new HashMap<String, Object>();
        mTmp0.put("label","全部");
        mTmp0.put("value", "全部");
        l1.add(mTmp0);
        for(Map<String, Object> map:list){
            String exModule=map.get("exmodule").toString();
            String excode=map.get("excode").toString();
            Map<String, Object> mTmp1=new HashMap<String, Object>();
            l1.add(mTmp1);
            mTmp1.put("label",exModule);
            mTmp1.put("value",exModule);
            List l2=new ArrayList();
            mTmp1.put("children",l2);
            Map<String, Object> mTmp2=new HashMap<String, Object>();
            mTmp2.put("label","全部");
            mTmp2.put("value", "全部");
            l2.add(mTmp2);
            String[] labels=excode.split(",");
            for(String label:labels){
                Map<String, Object> mTmp3=new HashMap<String, Object>();
                mTmp3.put("label",label);
                mTmp3.put("value",label.split(":")[0]);
                l2.add(mTmp3);
            }
        }
        return l1;
    }

    @Override
    public Map queryEx(String module, String type, String timeStart, String timeEnd, String queryKey, int currentPage) throws VenusException {
        Map map=new HashMap<>();
        if(StringUtil.isEmpty(module)){
            return map;
        }
        if(StringUtil.isEmpty(type)){
            return map;
        }
        if(StringUtil.isEmpty(timeStart)){
            return map;
        }
        if(StringUtil.isEmpty(timeEnd)){
            return map;
        }


        String qModule="全部";
        String code="全部";
        String[] modules=module.split(",");
        if(modules!=null&&modules.length>1){
            qModule=modules[0];
            code=modules[1];
        }
        int qCode=0;
        try {
            qCode=Integer.parseInt(code);
        }catch (Exception ex){
        }

        String sql0="SELECT count(*) num FROM t_ex_log ex LEFT JOIN t_ex_log_par exp ON exp.`code` = ex.`code` LEFT JOIN t_user u ON u.id = ex. USER WHERE ex.Create_time BETWEEN ? AND ?";
        String sql1="SELECT date_format( ex.Create_time, '%y-%m-%d %T' ) time, ex.`code`, ex.msg, exp.module, exp.`comment`, ex.type, u.`name` uname FROM t_ex_log ex LEFT JOIN t_ex_log_par exp ON exp.`code` = ex.`code` LEFT JOIN t_user u ON u.id = ex. USER WHERE ex.Create_time BETWEEN ? AND ?";
        String sql2=" ORDER BY time DESC LIMIT ?, ?";
        List<Object> obj=new ArrayList<Object>();
        try {
            obj.add(DateUtil.dealDateFormat1(timeStart));
            obj.add(DateUtil.dealDateFormat1(timeEnd));
        } catch (ParseException e) {
            throw new VenusException(VenusResponseStatus.LOG_EXCEPTION, "时间格式有误!");
        }
        if(qCode!=0){
            //根据code查询
            sql0+=" and ex.`code`=?";
            sql1+=" and ex.`code`=?";
            obj.add(qCode);
        }else{
            if(!"全部".equals(qModule)){
                sql0+=" and exp.module=?";
                sql1+=" and exp.module=?";
                obj.add(qModule);
            }
        }

        if(!"全部".equals(type)){
            sql0+=" and ex.type=?";
            sql1+=" and ex.type=?";
            obj.add(type);
        }
        //
        if(!StringUtil.isEmpty(queryKey)){
            sql0+=" and CONCAT(ex.`code`,IFNULL(ex.msg,''),exp.module,exp.`comment`,ex.type,IFNULL(u.`name`,'')) like ?";
            sql1+=" and CONCAT(ex.`code`,IFNULL(ex.msg,''),exp.module,exp.`comment`,ex.type,IFNULL(u.`name`,'')) like ?";
            obj.add("%" +queryKey+"%");
        }
        List<Map<String, Object>> r=jdbcTemplate.queryForList(sql0,obj.toArray());
        int count=Integer.parseInt(r.get(0).get("num").toString());
        int r0=count/PAGE_SIZE;
        int r1=count%PAGE_SIZE;
        if(r1!=0){
            r0+=1;
        }
        map.put("count",count);
        map.put("totalPage", r0);
        obj.add((currentPage - 1) * PAGE_SIZE);
        obj.add(PAGE_SIZE);
        r=jdbcTemplate.queryForList(sql1+sql2,obj.toArray());
        map.put("items",r);
        return map;
    }
}